package org.jg.arkanoid.gameobjects;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jg.arkanoid.engine.collider.CollisionEvent;

public class Ball extends MovingObject{

	private static Logger LOG = LogManager.getLogger("Ball.class");
	
	private int diameter;
	
	public int getDiameter() {
		return diameter;
	}

	public void setDiameter(int diameter) {
		this.diameter = diameter;
		height = diameter;
		width = diameter;
	}
	
	@Override
	public void move() {
		position = position.addVector(velocity);
	}
	
	@Override
	public void callCollision(CollisionEvent event) {
		final double EPSILON = 0.0000001;
		final double CIRCLE_DEEGREES =360;
		
		double turnAngle = -Math.PI - 2*velocity.xAngle() - 2*event.getCollisionVector().xAngle();
		
		double multiple2Pi = Math.toDegrees(turnAngle) % CIRCLE_DEEGREES;
		if(multiple2Pi >= -EPSILON && multiple2Pi <= EPSILON)
		{
			turnAngle = Math.PI;
		}
		
		LOG.info("Collision. Vector turn angle: " + Math.toDegrees(turnAngle) );
		
		velocity = velocity.turnVector( turnAngle );
	}
	
	@Override
	public void setHeight(int height) {
		this.height = height;
		diameter = Math.max(height, width);
	}

	@Override
	public void setWidth(int width) {
		this.width = width;
		diameter = Math.max(height, width);
	}
	
}
